import tester.cobertura.Instrument;

/**
 * @author Adam Blackwell & Ryan Cassidy
 */
// to represent a line of credit account in a bank
@Instrument 
public class Credit extends AAccount {

	public int creditLine; // the total amount of money they may have access to.
	public double interest; // The APR at which they are charged

	public Credit(int accountNum, int balance, String name, int creditLine,
			double interest) {
		super(accountNum, balance, name);
		this.creditLine = creditLine;
		this.interest = interest;

	}

	/*
	 * TEMPLATE: FIELDS: ... accountNum ... -- int ... balance ... -- int ...
	 * name ... -- String ... creditLine ... -- int ... interest ... -- double
	 * 
	 * METHODS: ... deposit(int amt) ... -- IAccount ... withdraw(int amt) ...
	 * -- IAccount
	 */

	// produce an updated account after paying off the given amount
	@Override
	public IAccount deposit(int amt) {
		if (balance - amt < 0) {
			throw new RuntimeException("Cannot have a credit on your account.");
		} else {
			return new Credit(this.accountNum, this.balance - amt, this.name,
					this.creditLine, this.interest);
		}
	}

	// produce an updated account after borrowing the given amount
	@Override
	public IAccount withdraw(int amt) {
		if (balance + amt > creditLine) {
			throw new RuntimeException("Cannot spend above your credit line "
					+ this.name + ".");
		} else {
			return new Credit(this.accountNum, this.balance + amt, this.name,
					this.creditLine, this.interest);
		}
	}

	@Override
	// Checks if this account is a Credit account (It is).
	public boolean isCredit() {
		return true;
	}

	@Override
	// Turns the account into a Checking account.
	public Credit toCredit() {
		return this;
	}

	@Override
	// Checks if two accounts are the same.
	public boolean same(IAccount that) {
		if (that.isCredit()) {
			return that.toCredit().sameCredit(this);
		} else {
			return false;
		}
	}

	// use instanceOf
	// @Override
	// // Checks if two accounts are the same.
	// public boolean same(IAccount that) {
	// if(that.isCredit()){
	// return that.toCredit().sameCredit(this);
	// } else {
	// return false;
	// }
	// }

	// Compares the fields of two Credit accounts.
	protected boolean sameCredit(Credit that) {
		return this.name.equals(that.name) && this.balance == that.balance
				&& this.accountNum == that.accountNum
				&& this.creditLine == that.creditLine
				&& this.interest == that.interest;
	}
}
